﻿using DevExpress.XtraEditors;
using System.Windows.Forms;
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using DevExpress.XtraEditors.Controls;
using Mota.EditModule;

namespace Mota.XMLControls
{
    /// <summary>
    /// Represent ComboBox visual control based on DX LookUpEdit control
    /// </summary>
    public class DXGuiComboBox:GuiComboBox
    {
        /// <summary>
        /// List for storing static values if given
        /// </summary>
        private List<string> staticItems = null;
        /// <summary>
        /// Create LookUpEdit, add editvalue changed event handler
        /// Set BestFitMode to BestFitResizePopup
        /// Set TextEditStyle to DisableTextEditor mode
        /// </summary>
        public DXGuiComboBox()
        {
            visualControl = new LookUpEdit();
            LookUpEdit lue = visualControl as LookUpEdit;
            lue.EditValueChanged += lue_EditValueChanged;
            lue.Properties.BestFitMode = BestFitMode.BestFitResizePopup;
            lue.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
        }
        /// <summary>
        /// Method to add static string item to private  list with staticItems
        /// </summary>
        /// <param name="item">represent single static item</param>
        public override void AddStaticItem(string item)
        {
            if(staticItems == null)
            {
                // initialize list if not initialized before
                staticItems = new List<string>();
                (visualControl as LookUpEdit).Properties.DataSource = staticItems;
            }
            staticItems.Add(item);
        }
        /// <summary>
        /// Event handler for EditValueChanged event
        /// Only for staticItems purpose
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">EventArgs</param>
        private void lue_EditValueChanged(object sender, EventArgs e)
        {
            var binding = (sender as LookUpEdit).DataBindings["EditValue"];
            if (binding != null)
                binding.WriteValue();
            
            this.ChangeEventCall(null);
        }
        /// <summary>
        /// Getter return Text propertie of the LookUpEdit object
        /// Setter set Text propertie of the LookUpEdit object 
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as LookUpEdit).Text;
            }
            set
            {
                (visualControl as LookUpEdit).Text = value;
            }
        }
        /// <summary>
        /// Binding object for LookUpEdit
        /// getter return LookUpEdit.Properties.DataSource  value
        /// setter lookup value as BindingSource and try to parse values using _KeyField and _Fields attributes
        /// and add Range of columns
        /// </summary>
        public override object Binding
        {
            get
            {
                return (visualControl as LookUpEdit).Properties.DataSource;
            }
            set
            {
                // set available values to the list using BindingSource
                if (value is System.Windows.Forms.BindingSource)
                {
                    DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit lueProp  = (visualControl as LookUpEdit).Properties;
                    lueProp.DataSource = value;
                    lueProp.ValueMember = XmlAttributes["_KeyField"];
                    var columns = XmlAttributes["_Fields"].Split(',')
                                    .Where(field => field != lueProp.ValueMember)
                                    .Select(field => new LookUpColumnInfo(field,0))
                                    .ToArray();
                    lueProp.Columns.AddRange(columns);
                    lueProp.DisplayMember = XmlAttributes["_Fields"].Split(',').Last();
                    //lueProp.GetNotInListValue += lueProp_GetNotInListValue;
                }
            }
        }

        /// <summary>
        /// Add DataBinding to LookUpEdit EditValue propertie
        /// </summary>
        /// <param name="valueBinding">main binding with current record values</param>
        public override void SetValue(object valueBinding)
        {
            // set control value using valueBinding
            if ((visualControl as LookUpEdit).Properties.DataSource != null)
            {
                if ((valueBinding is BindingSource) && ((visualControl as LookUpEdit).DataBindings["EditValue"] == null))
                    (visualControl as LookUpEdit).DataBindings.Add("EditValue", 
                                    valueBinding as System.Windows.Forms.BindingSource,
                                    XmlAttributes["XMLAttribute"]);
            }
        }
        public override void Refresh(ConnectionConfigs configs,BindingSource mainBs)
        {
            this.Rfsh(mainControls, mainBs, configs);
        }

        public override object GetValue()
        {
            //if(this.XmlAttributes.ContainsKey("AttributeType"))
            //{
            //    SqlDbType type;
            //    if (Enum.TryParse(this.XmlAttributes["AttributeType"], out type))
            //    {
            //        if((type == SqlDbType.UniqueIdentifier)
            //            &&(String.IsNullOrEmpty((visualControl as LookUpEdit).EditValue.ToString())))
            //            return DBNull.
                        
            //    }

            //}
            return (visualControl as LookUpEdit).EditValue;
        }
    }
}
